package com.buba.system.controller;

import com.buba.system.pojo.Employee;
import com.buba.system.pojo.Page;
import com.buba.system.service.AccountService;
import com.buba.system.service.EmployeeService;
import com.buba.system.service.JobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.awt.print.Book;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/Employee")
public class EmployeeController {
    private EmployeeService employeeService;
    private JobService jobService;
    private AccountService accountService;
    /**
     * 需要分页，需要有条件查询
     *  查询
     * @return
     */
    @RequestMapping("/selAllEmployee")
    public Page selAllEmployee(@RequestParam Map<String, Object> map) {
        Page<Employee> page = new Page<>(Integer.parseInt(map.get("pageNow").toString()));
        map.put("page", page);
        String jobTitle = map.get("jobTitle").toString();
        String employeeName = map.get("employeeName").toString();
        List<Employee> list = null;
        // 没有根据职位进行查询 查询所有员工
        if ("".equals(jobTitle) && "".equals(employeeName)) {
            // 没有条件查询
            page.computePageTotal(employeeService.selCountAll());
            list = employeeService.selEmployeeAll(page);
        } else { // 携带查询条件
            page.computePageTotal(employeeService.selCount(map));
            list = employeeService.selAllEmployee(map);
        }
        list.forEach(e -> {
            e.setJobList(jobService.selJobByEmployeeId(e.getEmployeeId()));
        });
        page.setList(list);
        return page;
    }

    /**
     * @RequestParam 相当于Servlet中request.getParameter()
     * 添加
     * @return
     */
    @RequestMapping("/addEmployee")
    public Map<String,Object> addEmployee(@RequestParam Map<String, Object> map,String [] job){
        //时间戳 指定格式开头加上当前的事件作为id
        String employeeId = System.currentTimeMillis()+"";
        map.put("employeeId",employeeId);
        map.put("jobs",job);
        //1、添加员工信息 返回添加的条数
        Integer count = employeeService.addEmployee(map);
        if(count>0){
            //2、给员工添加职位
            long jobCount = employeeService.addJob(map);
            //3、给员工添加登录信息 账号 身份证号 密码 身份证的后6位
            String employeeIdCard = map.get("employeeIdCard").toString();
            map.put("password",employeeIdCard.substring(12));//截取字符串
            long userCount = accountService.addSystemUser(map);
            map.put("message","添加成功");
        }else {
            map.put("message","添加失败");
        }

        return map;
    }

    /**
     * 删除
     * @param
     * @return
     */
    @RequestMapping("/delEmployeeAll")
    public Map<String,Object> delEmployee(String ids){
        Map<String,Object> map = new HashMap<>();
        String[] split = ids.split(",");
        List<String> list = Arrays.asList(split);
        //先删除中间表外键
        Integer empJob = jobService.delJobByEmployeeId(list);
        //账号
        Integer system = accountService.delsystemAll(list);
        //员工
        Integer employee = employeeService.delEmployeeAll(list);
        if (employee != null){
            map.put("message","删除"+employee+"条数据");
        }else{
            map.put("message","删除失败");
        }
        return map;

    }

    /**
     * 修改员工信息
     * @param map
     * @return
     */
    @RequestMapping("/updEmployee")
    public Map<String, Object> updEmployee(@RequestParam Map<String, Object> map,String[] job) {
        // 1、点击修改按钮、触发修改窗口（回显数据）
        // 2、员工的基本信息、员工的职位信息（根据员工id）
        Integer updCount = employeeService.updEmployee(map);
        // 3、员工的职位修改（1、把它现有的职位全部删除。2、再全部添加）
        Integer delJobCount = jobService.delJobByEmployeeId(Arrays.asList(new String[]{map.get("employeeId").toString()}));
        map.put("jobs",job);
        Integer addJobCount = jobService.addJobByEmployeeId(map);
        return map;
    }



    @Autowired
    public void setAccountService(AccountService accountService) {
        this.accountService = accountService;
    }

    @Autowired
    public void setJobService(JobService jobService) {
        this.jobService = jobService;
    }

    @Autowired
    public void setEmployeeService(EmployeeService employeeService) {
        this.employeeService = employeeService;
    }
}
